package com.wannengbang.cloudleader.homepage;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.gyf.immersionbar.ImmersionBar;
import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.BaseActivity;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.MerchantNumBean;
import com.wannengbang.cloudleader.homepage.model.HomePageModelImpl;
import com.wannengbang.cloudleader.homepage.model.IHomePageModel;
import com.wannengbang.cloudleader.utils.DateTimeUtil;

import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 商户列表
 */
public class MerchantMonthListActivity extends BaseActivity {


    @BindView(R.id.ll_left)
    LinearLayout llLeft;
    @BindView(R.id.tv_rightText)
    TextView tvRightText;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.tv_total_num)
    TextView tvTotalNum;
    @BindView(R.id.tv_look_details)
    TextView tvLookDetails;
    @BindView(R.id.tv_gt_five_count)
    TextView tvGtFiveCount;
    @BindView(R.id.ll_look1)
    LinearLayout llLook1;
    @BindView(R.id.tv_gt_one_count)
    TextView tvGtOneCount;
    @BindView(R.id.ll_look2)
    LinearLayout llLook2;
    @BindView(R.id.tv_gt_three_count)
    TextView tvGtThreeCount;
    @BindView(R.id.ll_look3)
    LinearLayout llLook3;
    @BindView(R.id.tv_lt_five_count)
    TextView tvLtFiveCount;
    @BindView(R.id.ll_look0)
    LinearLayout llLook0;
    private IHomePageModel homePageModel;
    private String cycleType;
    private String param;
    private String start_time = "";
    private String end_time = "";
    private Calendar selectedDate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_merchant_month_list);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .statusBarDarkFont(false)
                .navigationBarDarkIcon(true)
                .navigationBarColor(R.color.white)
                .statusBarDarkFont(false, 0.2f)
                .init();
        initView();
        initData();
    }

    private void initView() {
        selectedDate = Calendar.getInstance();
        cycleType = getIntent().getStringExtra("cycleType");
        param = getIntent().getStringExtra("param");
        tvRightText.setText(param);
        homePageModel = new HomePageModelImpl();
        llRight.setOnClickListener(v -> {
            selectedDate.setTime(DateTimeUtil.parse23(param));
            selectTime();
        });
        llLeft.setOnClickListener(v -> {
            finish();
        });
    }

    private void initData() {
        requestGetStoreStatistics();
    }

    public void requestGetStoreStatistics() {
        String pay_date = param.replace(".", "-");
        homePageModel.requestReportStoreCount(pay_date, new DataCallBack<MerchantNumBean>() {
            @Override
            public void onSuccessful(MerchantNumBean bean) {
                tvLtFiveCount.setText(bean.getData().getMonth_trade_lt_five() + "");
                tvGtFiveCount.setText(bean.getData().getMonth_trade_five() + "");
                tvGtOneCount.setText(bean.getData().getMonth_trade_one() + "");
                tvGtThreeCount.setText(bean.getData().getMonth_trade_Three() + "");
                tvTotalNum.setText(String.valueOf(bean.getData().getAll_count()));
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    private TimePickerView pvTime;

    public void selectTime() {
        //这里可以更改参数，有好多类型
        Calendar startDate = Calendar.getInstance();
        Calendar endDate = Calendar.getInstance();
        startDate.set(1900, 0, 1);
        endDate.set(2099, 11, 31);
        pvTime = new TimePickerBuilder(mActivity, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                param = DateTimeUtil.format22(date);
                tvRightText.setText(param);
                requestGetStoreStatistics();
            }
        }).setType(new boolean[]{true, true, false, false, false, false})
                .setSubmitColor(getResources().getColor(R.color.text_color_2))//确定按钮文字颜色
                .setSubCalSize(14)
                .setCancelColor(getResources().getColor(R.color.text_color_2))//取消按钮文字颜色
                .setTitleBgColor(getResources().getColor(R.color.white))
                .setDividerColor(getResources().getColor(R.color.line_color_gray))
                .setTextColorCenter(getResources().getColor(R.color.text_color_2))
                .setContentTextSize(15)
                .setLineSpacingMultiplier(2)
                .setDate(selectedDate)// 如果不设置的话，默认是系统时间*/
                .setRangDate(startDate, endDate)//起始终止年月日设定
                .isDialog(true) //默认设置false ，内部实现将DecorView 作为它的父控件。
                .build();

        Dialog mDialog = pvTime.getDialog();
        if (mDialog != null) {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            pvTime.getDialogContainerLayout().setLayoutParams(params);

            Window window = mDialog.getWindow();
            if (window != null) {
                window.setWindowAnimations(R.style.picker_view_slide_anim);//修改动画样式
                window.getDecorView().setPadding(0, 0, 0, 0);
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
                lp.gravity = Gravity.BOTTOM;
            }
        }
        pvTime.show();
    }

    @OnClick({R.id.tv_look_details, R.id.ll_look0, R.id.ll_look1, R.id.ll_look2, R.id.ll_look3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_look_details:
                Intent intent = new Intent(mActivity, MerchantInfoMonthListActivity.class);
                intent.putExtra("titleName", "商户列表");
                intent.putExtra("cycleType", "all");
                intent.putExtra("param", "2018-01-01");
                intent.putExtra("type", 0);
                startActivity(intent);
                break;
            case R.id.ll_look0:
                intent = new Intent(mActivity, MerchantInfoMonthListActivity.class);
                intent.putExtra("titleName", "商户列表");
                intent.putExtra("cycleType", "month");
                intent.putExtra("param", param);
                intent.putExtra("type", 1);
                startActivity(intent);
                break;
            case R.id.ll_look1:
                intent = new Intent(mActivity, MerchantInfoMonthListActivity.class);
                intent.putExtra("titleName", "商户列表");
                intent.putExtra("cycleType", "month");
                intent.putExtra("param", param);
                intent.putExtra("type", 2);
                startActivity(intent);
                break;
            case R.id.ll_look2:
                intent = new Intent(mActivity, MerchantInfoMonthListActivity.class);
                intent.putExtra("titleName", "商户列表");
                intent.putExtra("cycleType", "month");
                intent.putExtra("param", param);
                intent.putExtra("type", 3);
                startActivity(intent);
                break;
            case R.id.ll_look3:
                intent = new Intent(mActivity, MerchantInfoMonthListActivity.class);
                intent.putExtra("titleName", "商户列表");
                intent.putExtra("cycleType", "month");
                intent.putExtra("param", param);
                intent.putExtra("type", 4);
                startActivity(intent);
                break;
        }
    }
}
